﻿
''' <summary>
''' Class đọc thông tin Version
''' </summary>
''' <remarks></remarks>
Public Class ApplicationVersion
    Friend Const mscSeperateToken As String = "."

    Public Shared Function CheckValidVersionString(ByVal sVersion As String) As Boolean
        If sVersion Is Nothing OrElse String.IsNullOrEmpty(sVersion) Then
            Return False
        Else
            Dim strNumber As String() = Nothing
            strNumber = sVersion.Split(mscSeperateToken)

            If strNumber Is Nothing OrElse strNumber.Length <> 4 Then
                Return False
            Else
                Dim iTry As Integer = 0
                For i As Integer = 0 To 3
                    If Not Integer.TryParse(strNumber(i), iTry) Then
                        Return False
                    Else
                        Select Case i
                            Case 0
                                If Integer.Parse(strNumber(i)) <> 1 Then
                                    Return False
                                End If
                            Case 1
                                If Integer.Parse(strNumber(i)) > 3 OrElse Integer.Parse(strNumber(i)) < 0 Then
                                    Return False
                                End If
                            Case 2, 3
                                If Integer.Parse(strNumber(i)) <= 0 Then
                                    Return False
                                End If
                        End Select
                    End If
                Next
            End If

        End If

        Return True
    End Function

    ''' <summary>
    ''' Lay thanh phan Version
    ''' </summary>
    ''' <param name="sVersion"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetIntegerElement(ByVal sVersion As String) As Integer()
        If CheckValidVersionString(sVersion) Then
            Dim arrInteger = New Integer(4) {}
            Dim strNumber As String() = sVersion.Split(mscSeperateToken)
            For i As Integer = 0 To 3
                arrInteger(i) = Integer.Parse(strNumber(i))
            Next

            Return arrInteger
        End If

        Return Nothing
    End Function

    Public Shared Function ConvertVersionToLong(ByVal sVersion As String) As Long
        Dim arrNumber As Integer() = GetIntegerElement(sVersion)
        If arrNumber Is Nothing Then
            Throw New Exception("Chuỗi phiên bản <<" & sVersion & ">> không hợp lệ.")
        Else
            Dim iLong As Long = 0

            For i As Integer = 1 To 3
                iLong += arrNumber(i) * (3 - i) * 1000
            Next

            Return iLong
        End If
    End Function

    Public Shared Function GetVersionFriendName(ByVal sVersion As String) As String
        Dim arrNumber As Integer() = GetIntegerElement(sVersion)
        If arrNumber Is Nothing Then
            Throw New Exception("Chuỗi phiên bản <<" & sVersion & ">> không hợp lệ.")
        Else
            Dim strName As String = String.Empty
            Select Case CType(arrNumber(1), ReleaseMode)
                Case ReleaseMode.Alpha
                    strName += "Alpha "
                Case ReleaseMode.Beta
                    strName += "Beta "
                Case ReleaseMode.R
                    strName += "R"
                Case ReleaseMode.RC
                    strName += "RC"

            End Select

            strName += arrNumber(2).ToString

            If arrNumber(3) <> 0 Then
                strName += "." & arrNumber(3)
            End If

            Return strName
        End If
    End Function
End Class

Public Enum ReleaseMode As Integer

    Alpha = 0

    Beta = 1

    RC = 2

    R = 3
End Enum
